package com.exam.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Constants;
import com.exam.demo.common.Result;
import com.exam.demo.controller.dto.UserDTO;
import com.exam.demo.entity.Student;
import com.exam.demo.entity.Student;
import com.exam.demo.service.StudentService;
import com.exam.demo.util.TokenUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    StudentService studentService;

    @PostMapping("/login")
    public Result login(@RequestBody Student student) {
        String studentId = student.getStudentId();
        String studentPassword = student.getStudentPassword();
        if (StrUtil.isBlank(studentId) || StrUtil.isBlank(studentPassword)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        UserDTO user = studentService.login(student);
        return Result.success(user);
    }

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String studentName) {
    //        return Result.success(studentService.findPage(new Page<>(pageNum,pageSize),studentName));
        Page<Student> page = studentService.findPage(new Page<>(pageNum, pageSize), studentName);
        return Result.success(page);
    }

    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Student student) {
        long count = studentService.count(new QueryWrapper<Student>().eq("student_id", student.getStudentId()));
        if(count > 0){
            boolean update = studentService.update(student, new UpdateWrapper<Student>().eq("student_id", student.getStudentId()));
            if(update){
                return Result.success("修改成功");
            }
        }else{
            boolean save = studentService.save(student);
            if(save){
                return Result.success("添加成功");
            }
        }
        return Result.error("10000","编辑失败");
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(studentService.removeById(id));
    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, @RequestParam(value = "") String studentName) throws Exception {
        response = studentService.export(response, studentName);
    }

    /**
     * excel 导入
     *
     * @param file
     * @throws Exception
     */
    @PostMapping("/import")
    public Result imp(MultipartFile file) throws Exception {
        boolean b = studentService.imp(file);
        return Result.success(b); //false导入失败
    }
}
